/*
 *通用工具类
 */
alert(122)

getPath();

// 项目路径
var hostUrl = "";

/**
 * 项目路径
 */
function getPath() {
    debugger
    var pathName = document.location.pathname;
    var index = pathName.substr(1).indexOf("/");
    hostUrl = "http://" + location.host + pathName.substr(0, index + 1);
}

//关闭窗口
function closeWindow(){
	//关闭弹框
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	parent.layer.close(index); //再执行关闭
};

//关闭窗口 刷新
function closeWindowReload(){
	//关闭弹框
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	parent.layer.close(index); //再执行关闭
	parent.location.reload();
};

//获取url传值方法
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//获取url传值方法
function GetQueryStringEncode(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = (decodeURI(window.location.search.substr(1)).match(reg));
    if (r != null) return unescape(r[2]);
    return null;
}

// 去除两边空格
String.prototype.Trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

// 校验身份证号
function identityCodeValid(code) {
    var city = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江 ",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北 ",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏 ",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外 "
    };
    var tip = "";
    var pass = true;

    if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
        tip = "身份证号格式错误";
        pass = false;
    }

    else if (!city[code.substr(0, 2)]) {
        tip = "地址编码错误";
        pass = false;
    }
    else {
        //18位身份证需要验证最后一位校验位
        if (code.length == 18) {
            code = code.split('');
            //∑(ai×Wi)(mod 11)
            //加权因子
            var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
            //校验位
            var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
            var sum = 0;
            var ai = 0;
            var wi = 0;
            for (var i = 0; i < 17; i++) {
                ai = code[i];
                wi = factor[i];
                sum += ai * wi;
            }
            var last = parity[sum % 11];
            if (parity[sum % 11] != code[17]) {
                tip = "校验位错误";
                pass = false;
            }
        }
    }
    return pass;
}

/**
 * 给数组增加contains方法
 * @param val
 * @returns {boolean}
 */
Array.prototype.contains = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) {
            return true;
        }
    }
    return false;
};

/**
 * 字符串扩展 endWith 方法
 * @param str
 * @returns {boolean}
 */
String.prototype.endWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length) {
        return false;
    }
    if (this.substring(this.length - str.length)) {
        return true;
    } else {
        return false;
    }
    return true;
};

/**
 * 字符串扩展 startWith 方法
 * @param str
 * @returns {boolean}
 */
String.prototype.startWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length) {
        return false;
    }
    if (this.substr(0, str.length) == str) {
        return true;
    } else {
        return false;
    }
    return true;
};

/**
 * 实现容器Map
 * @author zhoudongliang
 */
function Map() {
    var map = function (key, value) {//键值对
        this.key = key;
        this.value = value;
    }
    var put = function (key, value) {//添加键值对

        this.arr[this.arr.length] = new map(key, value);
    }
    var remove = function (key) {//删除key="key"的键值对，返回value值
        for (var i = 0; i < this.arr.length; i++) {
            var temp = this.arr.pop();
            if (this.arr[i].key === key) {

                return this.arr[i].value;
            }
            this.arr.push(temp);
        }
        return null;
    }
    var getKey = function (value) {//返回key对应的value值
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].value === value)
                return this.arr[i].key;
        }
        return null;
    }
    var getValue = function (key) {//返回value对应的key值
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key)
                return this.arr[i].value;
        }
        return null;
    }
    var getSize = function () {//返回容器大小
        return this.arr.length;
    }

    var show = function () {//打印容器内容
        var string = "";
        for (var i = 0; i < this.arr.length; i++) {
            string += (this.arr[i].key + ":" + this.arr[i].value + "\n");
        }
        //alert(string);
    }

    //获取MAP中所有VALUE的数组（ARRAY）
    var values = function() {
        var arr = new Array();
        for (i = 0; i < this.arr.length; i++) {
            arr.push(this.arr[i].value);
        }
        return arr;
    };
    //获取MAP中所有KEY的数组（ARRAY）
    var keys = function() {
        var arr = new Array();
        for (i = 0; i < this.arr.length; i++) {
            arr.push(this.arr[i].key);
        }
        return arr;
    };

    this.arr = new Array();
    this.remove = remove;
    this.put = put;
    this.show = show;
    this.getKey = getKey;
    this.getValue = getValue;
    this.getSize = getSize;
    this.values = values;
    this.keys = keys;

}

/*
var map = new Map();
map.put("num", 1);
map.put("red", "2");
alert(map.getKey(1));
alert(map.remove("num"));
map.show();
*/

/**
 * 序列化方法 可以获得未选中的checkbox值
 * 为jquery.serializeArray()解决radio,checkbox未选中时没有序列化的问题
 * @returns {*}
 */
$.fn.ghostsf_serialize = function () {
    var a = this.serializeArray();
    var $radio = $('input[type=radio],input[type=checkbox]', this);
    var temp = {};
    $.each($radio, function () {
        if (!temp.hasOwnProperty(this.name)) {
            if ($("input[name='" + this.name + "']:checked").length == 0) {
                temp[this.name] = "";
                a.push({name: this.name, value: ""});
            }
        }
    });
    //console.log(a);
    return jQuery.param(a);
};

/**
 * 时间戳转日期
 * @param timestamp
 * @returns {string}
 */
function timestampToTime(timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y+M+D+h+m+s;
}

Date.prototype.Format = function(fmt)
{ //author:
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}

//java对象日期，转js日期
function timeStampToString(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
}

//java对象日期，转js日期
function timeStampString(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date;
}

/**
 * 判断天气是否为今天
 * @param str
 * @returns {boolean}
 */
function isToday(str){
    var d = new Date(str.replace(/-/g,"/"));
    var todaysDate = new Date();
    if(d.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)){
        return true;
    } else {
        return false;
    }
}

/**
 * 判断天气是否为昨天
 * @param theDate
 * @returns {boolean}
 */
function isYestday(str){
    var date = new Date(str.replace(/-/g,"/"));
    var today = new Date(); //今天
    var yestday = new Date(today - 24*3600*1000);// 昨天
    if(date.setHours(0,0,0,0) == yestday.setHours(0,0,0,0)){
        return true;
    } else {
        return false;
    }
    //return date.getTime() < today && yestday <= date.getTime();
}


